from fastapi import APIRouter

import numpy as np
import pandas as pd

router = APIRouter(prefix="/generating", tags=["data_generating"])

@router.get(
    "/regression_mesomeric",
    tags=["data_analysis"],
    description="生成中介数据、描述性统计"
)
async def regression_mesomeric_data():
    # 设置随机种子以便复现
    np.random.seed(42)

    # 生成样本大小
    n_samples = 200

    # 生成整数类型的自变量X
    X = np.random.randint(low=0, high=100, size=n_samples)

    # 生成中介变量M，并加入截距
    a = 0.5
    intercept_M = 10  # 设置截距项
    eM = np.random.normal(loc=0, scale=1, size=n_samples)
    M = intercept_M + a * X + eM
    M = np.round(M, 2)  # 保留两位小数

    # 生成因变量Y，并加入截距
    b = 0.3
    c = 0.4
    intercept_Y = 5  # 设置截距项
    eY = np.random.normal(loc=0, scale=1, size=n_samples)
    Y = intercept_Y + b * X + c * M + eY
    Y = np.round(Y, 2)  # 保留两位小数

    reg_me_data = pd.DataFrame({"X": X, "M": M, "Y": Y})

    # 检查数据描述性统计
    rg_describe = reg_me_data.describe()

    # return {"data": rg_describe.to_json(orient="records")}
    # to_dict
    return {"data": rg_describe.to_json(orient="index")}

